/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import ro.dta.idbi.interfaces.ui.ISchemaChooser;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.Transformation;

/**
 * Combo box displaying all the schemas in the AutoMed repository, together with
 * the option to display only end of pathway schemas
 * 
 * @author Tudor Dobrila
 * 
 */
public class SchemaChooserPanel extends JPanel implements ISchemaChooser {

	private static final long serialVersionUID = 6088143376427266444L;

	/**
	 * Model used for the schemas combo box
	 */
	private DefaultComboBoxModel<String> schemasModel;

	/**
	 * Combo box holding a list of all schemas
	 */
	private JComboBox<String> schemasBox;

	/**
	 * Schema chooser panel class constructor
	 */
	public SchemaChooserPanel() {
		this(null);
	}
	
	/**
	 * Schema chooser panel class constructor
	 * 
	 * @param schema
	 *            Name of the schema initially selected
	 */
	public SchemaChooserPanel(String schema) {
		setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
		setOpaque(false);

		schemasModel = new DefaultComboBoxModel<String>();
		schemasBox = new JComboBox<String>(schemasModel);
		add(schemasBox);
		boolean loadedAll = false;
		updateSchemasBox(loadedAll);

		if (schema != null) {
			boolean isOk = false;
			for (int i = 0; i < schemasModel.getSize(); i++) {
				if (schemasModel.getElementAt(i).equals(schema)) {
					isOk = true;
				}
			}

			if (!isOk) {
				loadedAll = true;
				updateSchemasBox(loadedAll);
			}

			schemasModel.setSelectedItem(schema);
		} else {
			if (schemasModel.getSize() > 0) {
				schemasBox.setSelectedIndex(0);
			}
		}

		final JCheckBox checkBox = new JCheckBox("Only end of pathways schemas");
		checkBox.setSelected(!loadedAll);
		checkBox.setOpaque(false);
		checkBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				updateSchemasBox(!checkBox.isSelected());
			}
		});
		add(checkBox);
	}

	/* (non-Javadoc)
	 * @see ro.dta.idbi.ui.ISchemaChooser#getSelectedItem()
	 */
	@Override
	public String getSelectedItem() {
		if (schemasBox.getSelectedIndex() != -1) {
			return schemasModel.getSelectedItem().toString();
		}

		return "";
	}

	/**
	 * Update the elements in the schemas box
	 * 
	 * @param showAll
	 *            True if all schemas should be displayed, false if only data
	 *            source or end of pathway schemas should be displayed
	 */
	private void updateSchemasBox(boolean showAll) {
		Schema[] schemas = Schema.getSchemas();
		schemasModel.removeAllElements();

		for (Schema s : schemas) {
			boolean endPathway = true;
			for (Transformation t : s.getTransformations()) {
				if (t.getSchemas()[0].equals(s)) {
					endPathway = false;
				}
			}

			if (showAll || (!showAll && endPathway)) {
				schemasModel.addElement(s.getName());
			}
		}

		if (schemasModel.getSize() > 0) {
			schemasBox.setSelectedIndex(0);
		}
	}

	/* (non-Javadoc)
	 * @see ro.dta.idbi.ui.ISchemaChooser#getSchemasBox()
	 */
	@Override
	public JComboBox<String> getSchemasBox() {
		return schemasBox;
	}
}
